package com.scalar.chiptrack.finance.openpo.actioncommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemList;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItem;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.ChipTrackExtendedAction;

/**
 * Created by IntelliJ IDEA.
 * Author: Phani
 * Date  : Feb 19, 2005
 * Time  : 2:29:09 PM
 * To change this template use Options | File Templates.
 */
public class PurchaseOrderAddLineItemCommand  extends ChipTrackExtendedAction
{
    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {

        HttpSession mSession 		= request.getSession();
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        PurchaseOrderForm purchaseOrderForm         = ( PurchaseOrderForm ) form;
        PurchaseOrderLineItemList purchaseOrderLineItemList = null;
        PurchaseOrderLineItem purchaseOrderLineItem = null ; //new PurchaseOrderLineItem();
        String actionForward 		= "Search";

		try
        {
            purchaseOrderLineItemList = ( PurchaseOrderLineItemList ) purchaseOrderForm.getPurchaseOrderLineItemList();

            purchaseOrderLineItem = new PurchaseOrderLineItem();
			purchaseOrderLineItem.setStatus( Constants.PURCHASE_ORDER_STATUS_NEW );

            //start: added by Murali Krishna, set the holds to approved by default if the purchase order if created from open po
            if( purchaseOrderForm.getOpenPurchaseOrder_Id() != -1 )
            {
                purchaseOrderLineItem.setFinanceHold(0);
                purchaseOrderLineItem.setOperationsHold(0);
            }
            //end: Murali Krishna

            purchaseOrderLineItem.setPurchaseOrderLineItemNumber( purchaseOrderLineItemList.size() + 1 );

            purchaseOrderLineItemList.add(purchaseOrderLineItem );
            request.setAttribute( "purchaseOrderTypesList", PurchaseOrderDAO.loadPurchaseOrderTypes() );
            request.setAttribute( "processNameList", PurchaseOrderDAO.loadProcessNames( purchaseOrderForm.getVendor_id() ) );
            request.setAttribute( "packageTypeList", PurchaseOrderDAO.loadPackageTypeList() );
            request.setAttribute ( "financeReasonCodes", PurchaseOrderDAO.loadFinanceHolds() );
            request.setAttribute ( "opsReasonCodes", PurchaseOrderDAO.loadOperationsHolds() );
            request.setAttribute( "poTaxes", PurchaseOrderDAO.loadPOTaxesList () );
            String searchType = request.getParameter("searchType");
            request.setAttribute( "searchType", searchType);
            
            PurchaseOrderDAO.loadLineItemPrice( null, purchaseOrderLineItemList );

            actionForward = "NewManufacturingPO";
       	}
        catch ( Exception e )
        {
            Logger.error( ERROR_LOADING_PURCHASE_ORDER_LISTS, e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_PURCHASE_ORDER_LISTS );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( ERROR_LOADING_PURCHASE_ORDER_LISTS, e ) );
            actionForward = "Search";
        }
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );

		return mapping.findForward( actionForward );
    }

}
